<template>
  <div>
    <button @click="dialogFormVisable = true">打开</button>
    <el-dialog
      v-model="dialogFormVisable"
      :before-close="beforeClose"
      title="添加权限"
      width="500"
    >
      <el-form
        ref="formRef"
        label-width="100px"
        label-position="left"
        :model="form"
      >
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请填写权限名称"></el-input>
        </el-form-item>
        <el-form-item label="权限" prop="permissions">
          <el-tree
            ref="treeRef"
            :data="treeData"
            style="max-width: 600px"
            node-key="id"
            show-checkbox
            :default-checked-keys="defaultKey"
          ></el-tree>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script setup>
import { onMounted, reactive, ref } from 'vue';
import { userGetMenu } from '../../../api/index';

onMounted(() => {
  userGetMenu().then((res) => {
    treeData.value = res.data.data;
    console.log(treeData.value);
  });
});

// 树形菜单权限结构
const treeData = ref([]);

// 默认勾选的tree
const defaultKey = [4, 5];
// 拿到tree实例
const treeRef = ref();

// form的数据
const form = reactive({ name: '', permissions_id: '' });

// 弹窗的显示隐藏
const dialogFormVisable = ref(false);

// 关闭弹窗的回调
const beforeClose = () => {
  dialogFormVisable.value = false;
};
</script>

<style></style>
